package com.baizan.daka.ui.activity

import android.content.Context
import android.content.Intent
import androidx.recyclerview.widget.GridLayoutManager
import com.baizan.base.BaseActivity
import com.baizan.daka.event.ReceiveTastEvent
import com.baizan.daka.help.ImgSelectHelp
import com.baizan.daka.mvp.model.bean.TaskInfoBean
import com.baizan.daka.showToast
import com.baizan.daka.ui.adapter.ImgAdapter
import com.baizan.daka.ui.contract.ReceiveTaskContract
import com.baizan.daka.ui.presenter.ReceiveTaskPresenter
import com.dianzan.daka.R
import kotlinx.android.synthetic.main.act_receive_task.*
import org.greenrobot.eventbus.EventBus
import java.io.File

class ReceiveTaskActivity : BaseActivity<ReceiveTaskPresenter>(), ReceiveTaskContract.View {
    var mTackPicHelper: ImgSelectHelp? = null
    val adapter = ImgAdapter()
    var taskInfo :TaskInfoBean?= null
    companion object {
        fun start(context: Context, taskInfoBean: TaskInfoBean) {
            val intent = Intent(context, ReceiveTaskActivity::class.java)
            intent.putExtra("taskinfo", taskInfoBean)
            context.startActivity(intent)
        }
    }

    override fun initPresenter(): ReceiveTaskPresenter = ReceiveTaskPresenter(this)

    override fun layoutId(): Int = R.layout.act_receive_task

    override fun initData() {
        mTackPicHelper = ImgSelectHelp(this)
        val taskInfo = intent.getSerializableExtra("taskinfo")
        taskInfo as TaskInfoBean
        this.taskInfo = taskInfo
        task_name.text = "任务名称：${taskInfo.taskName}"
        task_momey.text = "金额：${taskInfo.taskPrice}"
        val taskSource: String
        if (taskInfo.taskClass == 1) {
            taskSource = "抖音"
        } else {
            taskSource = "快手"
        }
        task_source.text = "所属媒体：${taskSource}"
        task_start_time.text = "领取时间：${taskInfo.receiveTime}"
        task_end_time.text = "结束时间：${taskInfo.overdueTime}"
        task_status.text = "任务状态：${taskInfo.taskStateName}"
        image_tips_tv.setOnClickListener {
            uploadImage()
        }
        image.setOnClickListener {
            uploadImage()
        }
        submit_bt.setOnClickListener {
            if(adapter.data.size<=0){
                showToast("请上传任务凭证!")
            }else{
                presenter?.submit(taskInfo.taskId, remarks.text.toString(), adapter.data)
            }

        }
    }

    private fun uploadImage() {
        //显示 获取图片对话框
        mTackPicHelper?.let {
            it.setCropImage(false)
            it.setSelectPicCount(2 -adapter.data.size)
            it.setPicCallback(object : ImgSelectHelp.PicCallback {
                override fun onFinish(list: List<String>) {
                    adapter.addData(list)
                    it.dismiss()
                }
            })
            it.createDialog().show()
        }
    }

    override fun initView() {
        val manager = GridLayoutManager(mContext, 3)
        recycler_view.setLayoutManager(manager)
        recycler_view.setAdapter(adapter)
    }

    override fun start() {
    }

    override fun submitSuccess(url: String) {
        taskInfo?.let {
            EventBus.getDefault().post(ReceiveTastEvent(it.id))
        }
        showToast(url)
        finish()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == ImgSelectHelp.CODE_GALLERY_REQUEST || requestCode == ImgSelectHelp.CODE_CAMERA_REQUEST || requestCode == ImgSelectHelp.CODE_RESULT_REQUEST
        ) {
            mTackPicHelper?.onActivityResult(requestCode, resultCode, data)
            return
        }
    }
}